package beautiful.butterfly.drds.schedule.strategy.factory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by ibm on 2018/1/20.
 */
class InitialThread extends Thread {
    private static transient Logger log = LoggerFactory.getLogger(InitialThread.class);
    ManageFactory manageFactory;
    boolean isStop = false;

    public InitialThread(ManageFactory manageFactory) {
        this.manageFactory = manageFactory;
    }

    public void stopThread() {
        this.isStop = true;
    }

    @Override
    public void run() {
        manageFactory.lock.lock();
        try {
            int count = 0;
            while (manageFactory.zooKeeperManager.checkZookeeperState() == false) {
                count = count + 1;
                if (count % 50 == 0) {
                    manageFactory.errorMessage = "Zookeeper connecting ......" + manageFactory.zooKeeperManager.getConnectStr() + " spendTime:" + count * 20 + "(ms)";
                    log.error(manageFactory.errorMessage);
                }
                Thread.sleep(20);
                if (this.isStop == true) {
                    return;
                }
            }
            manageFactory.initialData();
        } catch (Throwable e) {
            log.error(e.getMessage(), e);
        } finally {
            manageFactory.lock.unlock();
        }

    }

}
